package com.teng.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.teng.dao.RoleDao;
import com.teng.entity.RoleEntity;
import com.teng.exception.OAException;
import com.teng.service.RoleCrudService;
import com.teng.util.MD5;
import com.teng.util.Uuid;

public class RoleCrudServiceImpl implements RoleCrudService {
		@Override
		public void modifyPassword(String id, String oldPassword, String newPassword) throws Exception {
			if(oldPassword==null||(oldPassword=oldPassword.trim()).length()==0) {
				throw new OAException("旧密码不能为空");
			}
			if(newPassword==null||(newPassword=newPassword.trim()).length()==0) {
				throw new OAException("新密码不能为空");
			}
			RoleDao rd=new RoleDao();
			boolean a=rd.slectpassword(id,MD5.encode(oldPassword));
			if(a==false) {
				throw new OAException("密码错误");
			}
			rd.modifypassword(id,newPassword);
			
		}
		@Override
		public void updata(String id, String code, String name, String remark) throws Exception {
			if(id==null||(id=id.trim()).length()==0) {
				throw new OAException("错误");
			}
			if(name==null||(name=name.trim()).length()==0) {
				throw new OAException("名字不能为空");
			}
			if(code==null||(code=code.trim()).length()==0) {
				throw new OAException("账号不能为空");
			}
			
			if(code.length()<6||code.length()>32) {
				throw new OAException("账号长度必须在6位和32位之间");
			}
			RoleDao a=new RoleDao();
			if(a.validatecode(code)==true) {
				throw new OAException("代号已存在");
			}
		
		}
		@Override
		public void del(String id) throws Exception {
			if(id==null||(id=id.trim()).length()==0) {
				throw new OAException("id不能为空");
			}
			RoleDao rd=new RoleDao();
			Boolean a=rd.seleteId(id);
			if(a==true) {
				throw new OAException("不能删除");
			}
			rd.del(id);
			
		}
		@Override
		public RoleEntity check(String id) throws Exception {
			if(id==null||(id=id.trim()).length()==0) {
				throw new OAException("id不能为空");
			}
			RoleDao rd=new RoleDao();
			RoleEntity a=new RoleEntity();
			RoleEntity re=rd.check(id);
			return re;
		}
		
		
		@Override
		public List<RoleEntity> list(String id) throws Exception {
			RoleDao rd=new RoleDao();
			List<RoleEntity> re= rd.list(id);
			
		return  re;
		}
		
		
		@Override
		public RoleEntity insert(String code,String name, String remark,String creatId) throws Exception {
			if(code==null||(code=code.trim()).length()==0) {
				throw new OAException("代号不能为空");
			}
			if(name==null||(name=name.trim()).length()==0) {
				throw new OAException("名称不能为空");
			}
			
			RoleDao rd=new RoleDao();
			boolean b=rd.validatecode(code);
			if(b==true) {
				throw new OAException("代号已存在");
			}
			Pattern pattern = Pattern.compile("\\w+");
			 Matcher matcher = pattern.matcher(name);
			if(matcher.matches()==false) {
				throw new OAException("名称必须是中文");
			}
			
			
			RoleEntity re=new RoleEntity();
			re.setCode(code);
			re.setName(name);
			re.setRemark(remark);
			re.setId(Uuid.uuid());
			re.setCreateUserId(creatId);
			re.setCreateTime(new Date());
			
			rd.insert(re);
			
			return re;
		}
}
